package cn.com.haopy.yue.ai.wx.web;


import cn.com.haopy.yue.ai.core.enums.PayStatusCode;
import cn.com.haopy.yue.ai.core.system.SystemConfig;
import cn.com.haopy.yue.ai.core.utils.IpUtil;
import cn.com.haopy.yue.ai.core.utils.ResponseUtil;
import cn.com.haopy.yue.ai.core.validator.Order;
import cn.com.haopy.yue.ai.core.validator.Sort;
import cn.com.haopy.yue.ai.db.entity.arisk.AIRiskOrder;
import cn.com.haopy.yue.ai.db.entity.arisk.HOSUser;
import cn.com.haopy.yue.ai.db.service.hos.AIRiskOrderService;
import cn.com.haopy.yue.ai.db.service.hos.HOSUserService;
import cn.com.haopy.yue.ai.db.utils.GeneratorUtil;
import cn.com.haopy.yue.ai.wx.annotation.LoginUser;
import cn.com.haopy.yue.ai.wx.service.WxPayCommService;
import cn.com.haopy.yue.ai.wx.web.base.BaseWxController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * 登录
 */
@RestController
@RequestMapping("/haopyYueAi/order")
@RequiredArgsConstructor
@Slf4j
@Api(tags = "订单")
public class WxOrderController extends BaseWxController {
    @Autowired
    private WxPayCommService wxPayCommService;
    @Autowired
    private HOSUserService userService;

    @Autowired
    private AIRiskOrderService orderService;


    @GetMapping("/vipOrder")
    @ApiOperation(value = "创建订单")
    public Object vipOrder(@LoginUser Integer userId) throws Exception {
        if (Objects.isNull(userId)) {
            return ResponseUtil.unlogin();
        }
        AIRiskOrder order = new AIRiskOrder();
        order.setUserId(userId);
        order.setOrderPrice(SystemConfig.getVipChargeMoney());
        order.setMonths(SystemConfig.getVipchargeMonth());
        order.setOrderNo(GeneratorUtil.genOrderSn());
        Boolean bool = orderService.save(order);
        if (bool) {
            return ResponseUtil.ok(order.getOrderNo());
        }

        return ResponseUtil.failMsg("创建订单失败！");
    }

    @GetMapping("/wxPay")
    @ApiOperation(value = "支付")
    public Object wxPay(@LoginUser Integer userId,
                        @RequestParam @ApiParam("订单编号") String orderNo) throws Exception {

        if (Objects.isNull(userId)) {
            return ResponseUtil.unlogin();
        }

        AIRiskOrder order = orderService.getByOrderNo(orderNo);

        if (order == null) {
            return ResponseUtil.failMsg("订单不存在！");
        }

        if (!order.getPayStatus().equals(PayStatusCode.UNPAY.getCode())) {
            return ResponseUtil.failMsg("支付不在状态！");
        }


        HOSUser user = userService.getById(userId);

        if (user == null) {
            return ResponseUtil.failMsg("用户不存在！");
        }


        return wxPayCommService.wxPay(orderNo, user.getWeixinOpenid(), "会员费用支付", order.getOrderPrice(), IpUtil.getIpAddr(request));


    }

    @ApiOperation("充值-记录")
    @GetMapping("/list")
    public Object list(@LoginUser Integer userId,
                       @RequestParam(defaultValue = "1") Integer pageNo,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       @Sort @RequestParam(defaultValue = "create_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        if (Objects.isNull(userId)) {
            return ResponseUtil.unlogin();
        }
        Page<AIRiskOrder> list = orderService.querySelective(userId, null, pageNo, pageSize, sort, order);
        return ResponseUtil.okPage(list);
    }


}